package com.cloud.service.impl;

import com.cloud.model.entity.Payment;
import com.cloud.mapper.PaymentMapper;
import com.cloud.model.form.PaymentForm;
import com.cloud.service.PaymentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.cloud.common.utils.DateUtils;

import com.cloud.model.query.PaymentPageQuery;
import com.cloud.model.bo.PaymentBO;
import com.cloud.model.vo.PaymentPageVO;
import com.cloud.converter.PaymentConverter;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 支付表服务实现类
 *
 * @author Sailer Wen
 * @since 2024-07-24
 */
@Service
@RequiredArgsConstructor
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, Payment> implements PaymentService {

    private final PaymentConverter paymentConverter;

    /**
    * 获取支付表分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<PaymentPageVO>} 支付表分页列表
    */
    @Override
    public IPage<PaymentPageVO> listPagedPayments(PaymentPageQuery queryParams) {
    
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<PaymentBO> page = new Page<>(pageNum, pageSize);

        // 格式化为数据库日期格式，避免日期比较使用格式化函数导致索引失效
        DateUtils.toDatabaseFormat(queryParams, "startTime", "endTime");
    
        // 查询数据
        Page<PaymentBO> boPage = this.baseMapper.listPagedPayments(page, queryParams);
    
        // 实体转换
        return paymentConverter.bo2PageVo(boPage);
    }
    
    /**
     * 获取支付表表单数据
     *
     * @param id 支付表ID
     * @return
     */
    @Override
    public PaymentForm getPaymentFormData(Long id) {
        Payment entity = this.getById(id);
        return paymentConverter.entity2Form(entity);
    }
    
    /**
     * 新增支付表
     *
     * @param formData 支付表表单对象
     * @return
     */
    @Override
    public boolean savePayment(PaymentForm formData) {
        // 实体转换 form->entity
        Payment entity = paymentConverter.form2Entity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新支付表
     *
     * @param id   支付表ID
     * @param formData 支付表表单对象
     * @return
     */
    @Override
    public boolean updatePayment(Long id,PaymentForm formData) {
        Payment entity = paymentConverter.form2Entity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除支付表
     *
     * @param ids 支付表ID，多个以英文逗号(,)分割
     * @return true|false
     */
    @Override
    public boolean deletePayments(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的支付表数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
            //  .toList();
        return this.removeByIds(idList);
    }
    

}
